package main

import (
    "io/ioutil"
    "strings"
    "strconv"
)

const EMODE_PUSH string = "push"
const EMODE_PULL string = "pull"

type Config struct {
    Acl *IpAcl
    Listen string
    Mode string
    postResApi string
    getCmdApi string
    timeInterval int
}

func NewConfig() *Config {
    v := &Config{}

    v.Listen = ":8015"
    v.Mode = EMODE_PUSH
    v.timeInterval = 10

    return v
}

var g_config *Config

func loadConfig() *Config {
    c := NewConfig()

    buf, err := ioutil.ReadFile("/etc/vmrobot/default.ini")
    if err != nil {
        mainlog.Println("read config file failed.", err)
        return c
    }

    lines := strings.Split(string(buf), "\n")
    for _,line := range lines {
        line = strings.TrimSpace(line)
        if line == "" || line[0] == ';' {
            continue
        }

        if strings.Index(line, "acl=") == 0 {
            c.Acl = parseIpAcl(line[:4])
        } else if strings.Index(line, "listen=") == 0 {
            c.Listen = strings.TrimSpace(line[7:])
        } else if strings.Index(line, "mode=") == 0 {
            c.Mode = strings.TrimSpace(line[5:])
        } else if strings.Index(line, "postResApi=") == 0 {
            c.postResApi = strings.TrimSpace(line[11:])
        } else if strings.Index(line, "getCmdApi=") == 0 {
            c.getCmdApi = strings.TrimSpace(line[10:])
        } else if strings.Index(line, "timeInterval=") == 0 {
            var tmp string = strings.TrimSpace(line[13:])
            t, err := strconv.Atoi(tmp)
            if err != nil {
                mainlog.Println("timeInterval err", err.Error())
            } else {
                c.timeInterval = t
            }
        } 
    }

    return c
}